import { defineStore } from 'pinia';

export const useInfoStore = defineStore('info', {
	state: () => {
		return { 
			Authorization: uni.getStorageSync('Authorization') || false,
			userInfo:uni.getStorageSync('userInfo') || null,
			subjectAppList:[]
		};
	},
	// 也可以这样定义
	// state: () => ({ count: 0 })
	actions: {
		loginInit(res){
			this.Authorization=res.token
			this.userInfo=res.userInfo
			uni.setStorageSync('Authorization',res.token)
			uni.setStorageSync('userInfo',res.userInfo)
			
		},
		logout() {
			this.Authorization=false
			this.userInfo=null
			uni.removeStorageSync('Authorization')
			uni.removeStorageSync('userInfo')
			
		},
		getAppList(callback){
			uni.publicJs.post('/app/index?cmd=loadSubjectApp',{},
				res=>{
					callback(res.data)
				}
			)
		},
		updateUser() {
			// 存到状态中
			// uni.publicJs.post('/user/index?an=getUserInfo',{},
			// 	res=>{
			// 		if(res.code===200){
			// 			this.user=res.data
			// 		}
			// 	},{loading:false}
			// )			
		},
		tenBitTimestamp(time) {
		  const date = new Date(time * 1000)
		  const y = date.getFullYear()
		  let m = date.getMonth() + 1
		  m = m < 10 ? '' + m : m
		  let d = date.getDate()
		  d = d < 10 ? '' + d : d
		  let h = date.getHours()
		  h = h < 10 ? '0' + h : h
		  let minute = date.getMinutes()
		  let second = date.getSeconds()
		  minute = minute < 10 ? '0' + minute : minute
		  second = second < 10 ? '0' + second : second
		  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second //组合
		},
		secondTohours(seconds){
			var secondTime = parseInt(seconds);// 秒
			var minuteTime = '00';// 分
			var hourTime = '00';// 小时
			if(secondTime > 59) {//如果秒数大于60，将秒数转换成整数
			  minuteTime = this.addzero(parseInt(secondTime / 60))
			  secondTime = this.addzero(parseInt(secondTime % 60))
			  if(minuteTime > 59) {
			    //获取小时，获取分钟除以60，得到整数小时
			    hourTime = this.addzero(parseInt(minuteTime / 60))
			    //获取小时后取佘的分，获取分钟除以60取佘的分
			    minuteTime = this.addzero(parseInt(minuteTime % 60))
			  }
			}else{
			  secondTime=this.addzero(secondTime)
			}
					
			return hourTime+ ":" + minuteTime + ":" +secondTime
		},
		addzero(num){
		  return num < 10 ? '0'+num : num
		},

	},
});